package com.zbbt.sportslottery.ui.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ImageView.ScaleType;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.zbbt.sportslottery.R;
import com.zbbt.sportslottery.app.App;
import com.zbbt.sportslottery.bean.News;
import com.zbbt.sportslottery.bean.NewsStatus;
import com.zbbt.sportslottery.db.DbManager;
import com.zbbt.sportslottery.util.LogUtil;
import com.zbbt.sportslottery.util.TimeUtil;

public class HomeListAdapter extends BaseAdapter {

	private List<News> dataList;
	private int TYPE_COUNT = 10;
	private int currentType;
	private LayoutInflater layoutInflater;
	private boolean loadImg = true;
	private Context context;
	private OnBottomClickListener clickListener;

	@Override
	public int getCount() {
		if (dataList == null)
			return 0;
		else
			return dataList.size();
	}

	@Override
	public Object getItem(int position) {
		return dataList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public int getItemViewType(int position) {
		return dataList.get(position).getImgType();
	}

	@Override
	public int getViewTypeCount() {
		return TYPE_COUNT;
	}

	/**
	 * 2:纯文本，4:小图，5:大图，6:图集
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		NewsStatus newsStatus = null;
		News itemData = dataList.get(position);
		newsStatus = DbManager.getReadNews(itemData.getNewsId() + "");
		// 获取当前位置对应的type
		currentType = getItemViewType(position);
		switch (currentType) {
		case 2:// 纯文本
			TextOnlyItem textOnlyItem = null;
			if (convertView == null) {
				convertView = layoutInflater.inflate(R.layout.item_only_text, null);
				textOnlyItem = new TextOnlyItem();
				textOnlyItem.title = (TextView) convertView.findViewById(R.id.information_only_text_title);
				textOnlyItem.summary = (TextView) convertView.findViewById(R.id.information_summary);
				textOnlyItem.tTimer = (TextView) convertView.findViewById(R.id.information_timer);
				textOnlyItem.information_flag = (TextView) convertView.findViewById(R.id.information_flag);
				convertView.setTag(textOnlyItem);
			} else {
				textOnlyItem = (TextOnlyItem) convertView.getTag();
			}
			String strTimer;
			if (itemData != null) {
				strTimer = TimeUtil.getTimeInterval(itemData.getCreateAt() + "");
				textOnlyItem.tTimer.setText(strTimer);
				textOnlyItem.title.setText(itemData.getNewsTitle());
				textOnlyItem.summary.setText(itemData.getNewsModel().replace("\r\n", ""));
				if (newsStatus != null) {
					textOnlyItem.title.setTextColor(context.getResources().getColor(R.color.black_54));
					textOnlyItem.summary.setTextColor(context.getResources().getColor(R.color.black_54));
				} else {
					textOnlyItem.title.setTextColor(context.getResources().getColor(R.color.black_87));
					textOnlyItem.summary.setTextColor(context.getResources().getColor(R.color.black_54));
				}
				textOnlyItem.information_flag.setText(getNewsFlag(itemData.getNews_flag()));
			}
			break;
		case 1:
		case 3:
		case 4:
			SmallPictureItem smallPictureItem = null;
			if (convertView == null) {
				convertView = layoutInflater.inflate(R.layout.item_small_pic, null);
				smallPictureItem = new SmallPictureItem();
				smallPictureItem.title = (TextView) convertView
						.findViewById(R.id.information_small_pic_title);
				smallPictureItem.smallPic = (ImageView) convertView.findViewById(R.id.information_small_pic);
				smallPictureItem.information_time = (TextView) convertView
						.findViewById(R.id.information_time);
				convertView.setTag(smallPictureItem);
			} else {
				smallPictureItem = (SmallPictureItem) convertView.getTag();
				smallPictureItem.smallPic.setImageResource(R.drawable.img_load_fail);
			}
			if (itemData != null) {
				smallPictureItem.title.setText(itemData.getNewsTitle());
				strTimer = TimeUtil.getTimeInterval(itemData.getCreateAt() + "");
				if (getNewsFlag(itemData.getNews_flag()).equals("")) {
					smallPictureItem.information_time.setText(strTimer);
				} else {
					smallPictureItem.information_time.setText(strTimer + "  "
							+ getNewsFlag(itemData.getNews_flag()));
				}

				if (newsStatus != null) {
					smallPictureItem.title.setTextColor(context.getResources().getColor(R.color.black_54));
					smallPictureItem.information_time.setTextColor(context.getResources().getColor(
							R.color.black_54));
				} else {
					smallPictureItem.title.setTextColor(context.getResources().getColor(R.color.black_87));
					smallPictureItem.information_time.setTextColor(context.getResources().getColor(
							R.color.black_54));
				}
				if (loadImg)// 判断长度是被用户逼的，无图新闻作有图新闻上传是图片url只有ip，判断url长度是否大于25过滤掉这类假图
				{
					if (null != itemData.getNewsImg() && itemData.getNewsImg().size() > 0) {
						ImageLoader.getInstance().displayImage(itemData.getNewsImg().get(0).getImgUrl() + "",
								smallPictureItem.smallPic, App.option);
					} else {
						smallPictureItem.smallPic.setImageResource(R.drawable.img_load_fail);
					}

				}
			}
			break;

		case 5:// 大图
			BigPictureItem bigPictureItem = null;

			if (convertView == null) {
				convertView = layoutInflater.inflate(R.layout.item_big_pic, null);
				bigPictureItem = new BigPictureItem();
				bigPictureItem.title = (TextView) convertView.findViewById(R.id.information_big_pic_title);
				bigPictureItem.bitPic = (ImageView) convertView.findViewById(R.id.information_big_pic);
				bigPictureItem.bigTimer = (TextView) convertView.findViewById(R.id.information_big_timer);
				convertView.setTag(bigPictureItem);
			} else {
				bigPictureItem = (BigPictureItem) convertView.getTag();
				bigPictureItem.bitPic.setImageResource(R.drawable.img_load_fail);
			}
			String bigTimer;
			if (itemData != null) {
				bigPictureItem.title.setText(itemData.getNewsTitle());
				bigTimer = TimeUtil.getTimeInterval(itemData.getCreateAt() + "");
				if (getNewsFlag(itemData.getNews_flag()).equals("")) {
					bigPictureItem.bigTimer.setText(bigTimer);
				} else {
					bigPictureItem.bigTimer.setText(bigTimer + "  " + getNewsFlag(itemData.getNews_flag()));
				}
				if (newsStatus != null) {
					bigPictureItem.title.setTextColor(context.getResources().getColor(R.color.black_54));
				} else {
					bigPictureItem.title.setTextColor(context.getResources().getColor(R.color.black_87));
				}
				if (loadImg) {
					if (null != itemData.getNewsImg() && itemData.getNewsImg().size() > 0) {
						ImageLoader.getInstance().displayImage(itemData.getNewsImg().get(0).getImgUrl() + "",
								bigPictureItem.bitPic, App.option);
					} else {
						bigPictureItem.bitPic.setImageResource(R.drawable.img_load_fail);
					}
				}
			}
			break;
		case 6:// 图集
			AtlasItem atlasItem = null;
			if (convertView == null) {
				convertView = layoutInflater.inflate(R.layout.item_atlas, null);
				atlasItem = new AtlasItem();
				atlasItem.title = (TextView) convertView.findViewById(R.id.information_atlas_title);
				atlasItem.picLeft = (ImageView) convertView.findViewById(R.id.information_atlas_img_left);
				atlasItem.picMiddle = (ImageView) convertView.findViewById(R.id.information_atlas_img_middle);
				atlasItem.picRight = (ImageView) convertView.findViewById(R.id.information_atlas_img_right);
				atlasItem.atTimer = (TextView) convertView.findViewById(R.id.information_atls_timer);
				convertView.setTag(atlasItem);
			} else {
				atlasItem = (AtlasItem) convertView.getTag();
				atlasItem.picLeft.setImageResource(R.drawable.img_load_fail);
				atlasItem.picMiddle.setImageResource(R.drawable.img_load_fail);
				atlasItem.picRight.setImageResource(R.drawable.img_load_fail);
			}
			String atlsTimer;
			if (null != itemData) {
				atlasItem.title.setText(itemData.getNewsTitle());
				atlsTimer = TimeUtil.getTimeInterval(itemData.getCreateAt() + "");
				if (getNewsFlag(itemData.getNews_flag()).equals("")) {
					atlasItem.atTimer.setText(atlsTimer);
				} else {
					atlasItem.atTimer.setText(atlsTimer + "  " + getNewsFlag(itemData.getNews_flag()));
				}
				if (newsStatus != null) {
					atlasItem.title.setTextColor(context.getResources().getColor(R.color.black_54));
				} else {
					atlasItem.title.setTextColor(context.getResources().getColor(R.color.black_87));
				}
				if (loadImg && null != itemData.getNewsImg() && itemData.getNewsImg().size() > 0) {
					ImageLoader.getInstance().displayImage(itemData.getNewsImg().get(0).getImgUrl() + "",
							atlasItem.picLeft, App.option);
					ImageLoader.getInstance().displayImage(itemData.getNewsImg().get(1).getImgUrl() + "",
							atlasItem.picMiddle, App.option);
					ImageLoader.getInstance().displayImage(itemData.getNewsImg().get(2).getImgUrl() + "",
							atlasItem.picRight, App.option);
				}
			}
			break;
		}

		if (position == dataList.size() - 1) {
			if (clickListener != null) {
				clickListener.onBottomClick();
			}
		}
		LogUtil.defLog("getView----" + convertView);
		return convertView;
	}

	public String getNewsFlag(int num) {
		if (num == 1) {
			return "热";
		} else if (num == 2) {
			return "推荐";
		} else if (num == 3) {
			return "专家";
		}
		return "";
	}

	class TextOnlyItem {
		TextView tTimer;
		TextView title;
		TextView summary;
		TextView information_flag;
	}

	class BigPictureItem {
		TextView bigTimer;
		TextView title;
		ImageView bitPic;
	}

	class SmallPictureItem {
		TextView title;
		// TextView summary;
		ImageView smallPic;
		TextView information_time;
	}

	class AtlasItem {
		TextView atTimer;
		TextView title;
		ImageView picLeft;
		ImageView picMiddle;
		ImageView picRight;
	}

	class ExpertItem {
		ImageView headPic;
		TextView name;
		TextView goodAt;
	}

	/**
	 * @param dataList
	 *            要展示的数据
	 * @param context
	 *            上下文
	 */
	public HomeListAdapter(List<News> dataList, Context context) {
		this.dataList = dataList;
		this.context = context;
		layoutInflater = LayoutInflater.from(context);
	}

	public interface OnBottomClickListener {
		public void onBottomClick();
	}

	public void setOnBottomClickListener(OnBottomClickListener clickListener) {
		this.clickListener = clickListener;
	}
}
